package com.by;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author ServivE
 * @version 0.1
 * @className Productor
 * @description
 * @date 2024/10/21 9:11
 * @since jdk17
 */
public class Productor {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接工厂  创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setVirtualHost("erp");//虚拟主机  默认是 /
        factory.setPort(5672);//连接端口号
        factory.setHost("192.168.203.130");//服务器地址 默认是localhost
        factory.setUsername("admin");
        factory.setPassword("123456");

        //2创建连接  资源资源自动释放(连接 信道 关闭)
        try (Connection conn = factory.newConnection(); Channel channel = conn.createChannel()) {
            //3.声明交换器
            String exName = "topicLog";
            channel.exchangeDeclare(exName, BuiltinExchangeType.TOPIC);

            //4.声明队列  关注用户
            String q1 = "queueUser";
            channel.queueDeclare(q1, false, false, false, null);

            //关注错误
            String q2 = "queueError";
            channel.queueDeclare(q2, false, false, false, null);

            //关注订单错误
            String q3 = "orderError";
            channel.queueDeclare(q3, false, false, false, null);

            //5.绑定队列与交换器
            channel.queueBind(q1, exName, "*.user.*");
            channel.queueBind(q2, exName, "*.*.error");
            channel.queueBind(q3, exName, "*.order.error");

            //6.发送消息
            //队列q1  q2可以收到
            channel.basicPublish(exName, "sys.user.error", null, "用户登录异常".getBytes());
            //q2 q3
            channel.basicPublish(exName, "bus.order.error", null, "订单支付失败".getBytes());
            //q2
            channel.basicPublish(exName, "client.customer.error", null, "客户分配失败".getBytes());


        }

    }
}
